package com.ouzexi.finalexp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import com.ouzexi.finalexp.vo.registerReturn
import kotlinx.android.synthetic.main.activity_find_password.*
import okhttp3.*
import java.io.IOException

class find_password : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_find_password)
    }

    fun confirm(view: View) {
        // 获取输入信息
        val email = email.text
        val password = password.text
        val reg = reg.text

        // 判断
        if(email.toString().isEmpty()||password.toString().isEmpty()||reg.toString().isEmpty()){
            Toast.makeText(this,"请输入必填项", Toast.LENGTH_SHORT).show()
        } else{
            val urlAPI = "http://10.0.2.2:8080/school/student/findPassword"

            val requestBody: RequestBody = FormBody.Builder()
                .add("username", email.toString())
                .add("code", reg.toString())
                .add("password", password.toString())
                .build()

            val request: Request = Request.Builder()
                .url(urlAPI)
                .post(requestBody)
                .build()

            val client = OkHttpClient()
            client.newCall(request).enqueue(object : Callback {

                override fun onResponse(call: Call, response: Response) {
                    val result = response.body?.string()
                    println("result:$result")
                    val dataObj: registerReturn = Gson().fromJson(result, registerReturn::class.java)
                    if(dataObj.code == -1) {
                        Log.i("code",dataObj.code.toString())
                        Log.i("test","找回失败")
                        Looper.prepare()
                        Toast.makeText(this@find_password,dataObj.msg,Toast.LENGTH_SHORT).show()
                        Looper.loop()
                    }
                    else {
                        if (dataObj.msg == "success") {
                            Log.i("test","找回成功")
                            // 与数据库匹配
                            Looper.prepare()
                            Toast.makeText(this@find_password,"找回成功",Toast.LENGTH_SHORT).show()
                            val intent = Intent(this@find_password, login::class.java)
                            startActivity(intent)
                            Looper.loop()
                        } else {
                            Log.i("test","找回失败")
                            // 与数据库匹配
                            Looper.prepare()
                            Toast.makeText(this@find_password,"系统错误，请稍后重试",Toast.LENGTH_SHORT).show()
                            Looper.loop()
                        }
                    }
                }

                override fun onFailure(call: Call, e: IOException) {
                    println("Failed request api :( " + e.message)
                }
            })
        }
    }

    fun sendCode(view: View) {
        val email = email.text
        val urlAPI = "http://10.0.2.2:8080/school/student/send"

        val requestBody: RequestBody = FormBody.Builder()
            .add("to", email.toString())
            .build()

        val request: Request = Request.Builder()
            .url(urlAPI)
            .post(requestBody)
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {

            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                println("result:$result")
                Looper.prepare()
                Toast.makeText(this@find_password,"发送成功",Toast.LENGTH_SHORT).show()
                Looper.loop()
            }

            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })
    }
}